/* rqcfa.p    - REQUISITION APPROVAL - CHECK FOR FULL APPROVAL                */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/*V8:ConvertMode=NoConvert                                                    */
/* Revision: 8.6    LAST MODIFIED BY: 05/05/97  By: B. Gates          *J1Q2*  */
/* Revision: 8.5    LAST MODIFIED BY: 06/22/98  By: B. Gates          *J2QB*  */
/* Revision: 8.5    LAST MODIFIED BY: 02/08/99  By: Steve Nugent      *J396*  */
/* Revision: 9.1    LAST MODIFIED BY: 10/01/99  By: Jeff Wootton      *N014*  */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00 BY: *N0KP* myb                    */


/*                                                                          */
/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */
/*                                                                          */
/*         THIS PROGRAM IS A BOLT-ON TO STANDARD PRODUCT MFG/PRO.           */
/* ANY CHANGES TO THIS PROGRAM MUST BE PLACED ON A SEPARATE ECO THAN        */
/* STANDARD PRODUCT CHANGES.  FAILURE TO DO THIS CAUSES THE BOLT-ON CODE TO */
/* BE COMBINED WITH STANDARD PRODUCT AND RESULTS IN PATCH DELIVERY FAILURES.*/
/*                                                                          */
/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */
/*                                                                          */

         {mfdeclre.i}

         define input parameter p_req_nbr like rqm_mstr.rqm_nbr no-undo.
         define output parameter p_approvals_completed as log no-undo.
/*N014* /*J396*/ define variable temp_acct like rqd_acct no-undo. */

         {rqalttd.i} /*APPROVER LIST TEMPTABLE DEFINITIONS*/

         {rqconst.i}

         define variable any_denials as log no-undo.
         define variable approval_count as integer no-undo.
         define variable messages as character no-undo.
         define variable msglevels as character no-undo.
/*N014*/ define variable valid_acct like mfc_logical.

         {gprun.i ""rqpma.p""}
         find first rqf_ctrl no-lock.
         find first gl_ctrl no-lock.
         find rqm_mstr where rqm_nbr = p_req_nbr no-lock.
         p_approvals_completed = true.


         /*CHECK THAT ALL REQUIRED APPROVALS AND REVIEWALS HAVE BEEN MADE*/

         for each ttcal no-lock
         where ttcal_required:
            find first rqda_det
            where rqda_nbr = p_req_nbr
            and rqda_apr_userid = ttcal_apr_userid
            and rqda_action = ACTION_APPROVE
            no-lock no-error.

            if not available rqda_det then do:
               p_approvals_completed = false.
               return.
            end.
         end.



         /*CHECK FOR ANY DENIALS*/

         for each rqda_det no-lock
         where rqda_nbr = p_req_nbr
         and rqda_action = ACTION_DENY:
            p_approvals_completed = false.
            return.
         end.



         if rqm_direct then do:
            /*CHECK NUMBER OF DIRECT APPROVALS*/

            if rqf_diraprs_req > 0 then do:
               for each rqd_det no-lock
               where rqd_nbr = rqm_nbr
               and rqd_status = LINE_STATUS_OPEN,
               each pt_mstr no-lock
               where pt_part = rqd_part
               break by rqd_site by pt_prod_line:
                  if last-of(pt_prod_line) then do:
                     approval_count = 0.

                     for each ttdal no-lock
                     where ttdal_site = rqd_site
                     and ttdal_prod_line = pt_prod_line
                     and ttdal_is_approver:
                        find first rqda_det
                        where rqda_nbr = p_req_nbr
                        and rqda_apr_userid = ttdal_apr_userid
                        and rqda_action = ACTION_APPROVE
                        no-lock no-error.

                        if available rqda_det then do:
                           approval_count = approval_count + 1.
                        end.
                     end.

                     if approval_count < rqf_diraprs_req then do:
                        p_approvals_completed = false.
                        return.
                     end.
                  end.
               end.
            end.

         end.
         else do:
            /*CHECK NUMBER OF HORIZONTAL APPROVALS*/

            if rqf_horaprs_req > 0 then do:
               for each rqd_det no-lock
               where rqd_nbr = rqm_nbr
               and rqd_status = LINE_STATUS_OPEN
               and rqd_category > ""
               break by rqd_category:
                  if last-of(rqd_category) then do:
                     approval_count = 0.

                     for each tthal no-lock
                     where tthal_category = rqd_category
                     and tthal_is_approver:
                        find first rqda_det
                        where rqda_nbr = p_req_nbr
                        and rqda_apr_userid = tthal_apr_userid
                        and rqda_action = ACTION_APPROVE
                        no-lock no-error.

                        if available rqda_det then do:
                           approval_count = approval_count + 1.
                        end.
                     end.

                     if approval_count < rqf_horaprs_req then do:
                        p_approvals_completed = false.
                        return.
                     end.
                  end.
               end.
            end.



            /*COUNT NUMBER OF JOB APPROVALS*/

            approval_count = 0.

            if rqm_job > "" then do:
               for each ttjal where ttjal_is_approver no-lock:
                  /*NOTE WE JUST COUNT THE NUMBER OF JOB APPROVERS
                  AS THE COUNT IS USED FOR THE NEXT STEP*/

                  find first rqda_det
                  where rqda_nbr = p_req_nbr
                  and rqda_apr_userid = ttjal_apr_userid
                  and rqda_action = ACTION_APPROVE
                  no-lock no-error.

                  if available rqda_det then do:
                     approval_count = approval_count + 1.
                  end.
               end.
            end.



            /*CHECK NUMBER OF VERTICAL PLUS JOB APPROVALS*/

            if rqf_vrtaprs_req > 0 then do:
               /*NOTE WE DON'T RESET APPROVAL_COUNT BECAUSE JOB APPROVERS
               GET INCLUDED WITH VERTICAL APPROVERS*/

               for each ttval where ttval_is_approver no-lock:
                  find first rqda_det
                  where rqda_nbr = p_req_nbr
                  and rqda_apr_userid = ttval_apr_userid
                  and rqda_action = ACTION_APPROVE
                  no-lock no-error.

                  if available rqda_det then do:
                     approval_count = approval_count + 1.
                  end.
               end.

               if approval_count < rqf_vrtaprs_req then do:
                  p_approvals_completed = false.
                  return.
               end.
            end.
         end.



         /*CHECK ACCOUNT NUMBERS*/

         for each rqd_det no-lock
         where rqd_nbr = rqm_nbr
         and rqd_status = "":

/*N014* /*J396*/ temp_acct = substr(rqd_acct, 1, (8 - gl_sub_len)). */

            /*GPGLVERO.P RETURNS A LIST OF MESSAGE NUMBERS.
            IF ANY ERROR-LEVEL THEN THE REQ WILL NOT ACHIEVE
            FINAL APPROVAL.*/

/*J2QB*/    /* CHANGED RQD_CC TO RQM_CC IN FOLLOWING */
/*N014* - BEGIN DELETE
 *          {gprun.i ""gpglvero.p"" "(input rqd_acct, input ?,
 *          input rqm_cc, output messages, output msglevels)"}
 *N014* - END DELETE */

/*N014* - BEGIN REPLACE OLD VALIDATION CALL WITH NEW CALLS */
               {gprunp.i "gpglvpl" "p" "initialize"}
               {gprunp.i "gpglvpl" "p" "set_disp_msgs" "(input false)"}
               {gprunp.i "gpglvpl" "p" "validate_fullcode"
                   "(input rqd_acct,
                   input rqm_sub,
                   input rqm_cc,
                   input rqd_project,
                   output valid_acct)"}
               {gprunp.i "gpglvpl" "p" "get_msgs"
                   "(output messages, output msglevels)"}
/*N014* - END REPLACE OLD VALIDATION CALL WITH NEW CALLS */
            if lookup("3",msglevels) > 0 then do:
               p_approvals_completed = false.
               return.
            end.



/*N014* - BEGIN DELETE
 *          /*CHECK IF SUBACCT MATCHES HEADER SUBACCT*/
 *
 *          if rqm_sub > "" and gl_sub_len > 0 then do:
 *             if substr(rqd_acct, 9 - gl_sub_len) <> rqm_sub then do:
 *                p_approvals_completed = false.
 *                return.
 *             end.
 *          end.
 *N014* - END DELETE */



            /*CHECK CATEGORIES*/

            if rqd_category > "" then do:
               find last rqcd_det
               where rqcd_category = rqd_category
/*N014*        and rqcd_acct_from <= rqd_acct */
/*N014*        and rqcd_acct_to >= rqd_acct */
/*N014*/       and substring(rqcd_acct_from,1,8) + rqcd_sub_from
/*N014*/          <= substring(rqd_acct,1,8) + rqm_sub
/*N014*/       and substring(rqcd_acct_to,1,8) + rqcd_sub_to
/*N014*/          >= substring(rqd_acct,1,8) + rqm_sub
               no-lock no-error.

/*J396*/       if not available rqcd_det then do:

/*J396*/          for last rqcd_det where
/*J396*/                   rqcd_category = rqd_category   and
/*N014* /*J396*/                   rqcd_acct_from <= temp_acct    and */
/*N014* /*J396*/                   rqcd_acct_to >= temp_acct no-lock: */
/*N014*/                   rqcd_acct_from <= rqd_acct    and
/*N014*/                   rqcd_acct_to >= rqd_acct no-lock:
/*J396*/          end.

                  if not available rqcd_det then do:
                     p_approvals_completed = false.
                     return.
                  end.
/*J396*/       end. /* if not available rqcd_det */
            end.
         end.
